package com.huawei.testcloud.location;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.util.Log;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by hasee-pc on 2017/8/1.
 */

public class USBBroadcastReceiver  extends BroadcastReceiver {
    //代表USB设备的一个接口
    private UsbInterface mInterface;
    //代表一个接口的某个节点的类:写数据节点
    private UsbEndpoint usbEpOut;
    //代表一个接口的某个节点的类:读数据节点
    private UsbEndpoint usbEpIn;
    UsbDeviceConnection mDeviceConnection;

    @Override
    public void onReceive(Context context, final Intent intent) {
        if(intent.getAction().equals("android.hardware.usb.action.USB_STATE")){
            if (intent.getExtras().getBoolean("connected")){
                // usb 插入
                Toast.makeText(context, "插入", Toast.LENGTH_LONG).show();
                UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
                HashMap<String,UsbDevice> deviceHashMap =  usbManager.getDeviceList();

                if (deviceHashMap.isEmpty()){
                    Log.e("-----------------","deviceHashMap.isEmpty()");
                    Toast.makeText(context, "没有设备连接", Toast.LENGTH_LONG).show();
                    return;
                }
                Iterator<UsbDevice> iterator = deviceHashMap.values().iterator();
                while (iterator.hasNext()){
                    UsbDevice mUsbDevice = iterator.next();
                    Log.e("-----------------",mUsbDevice.getDeviceName());

                    //获取设备接口
                    for (int i = 0; i < mUsbDevice.getInterfaceCount(); ) {
                        // 一般来说一个设备都是一个接口，你可以通过getInterfaceCount()查看接口的个数
                        // 这个接口上有两个端点，分别对应OUT 和 IN
                        UsbInterface usbInterface = mUsbDevice.getInterface(i);
                        mInterface = usbInterface;
                        break;
                    }
                    //用UsbDeviceConnection 与 UsbInterface 进行端点设置和通讯
                    if (mInterface.getEndpoint(1) != null) {
                        usbEpOut = mInterface.getEndpoint(1);

                        Log.e("----------------1-",usbEpOut.getAddress()+"");
                    }
                    if (mInterface.getEndpoint(0) != null) {
                        usbEpIn = mInterface.getEndpoint(0);
                        Log.e("----------------0-",usbEpIn.getAddress()+"");
                    }
                    if (mInterface != null) {
                        // 判断是否有权限
                        if (usbManager.hasPermission(mUsbDevice)) {
                            // 打开设备，获取 UsbDeviceConnection 对象，连接设备，用于后面的通讯
                            mDeviceConnection = usbManager.openDevice(mUsbDevice);
                            if (mDeviceConnection == null) {
                                return;
                            }
                            if (mDeviceConnection.claimInterface(mInterface, true)) {
                                Log.e("--------------","找到设备接口");
                            } else {
                                mDeviceConnection.close();
                            }
                        } else {
                            Log.e("--------------","没有权限");
                        }
                    } else {
                        Log.e("--------------","没有找到设备接口");
                    }
                }
            }else{
                //   usb 拔出
            Toast.makeText(context, "拔出", Toast.LENGTH_LONG).show();
            }
        }

    }
}
